/*
 * Copyright 2019 Web3 Labs Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.web3j.abi;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Collectors;

import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.spi.FunctionEncoderProvider;
import org.web3j.crypto.Hash;
import org.web3j.utils.Numeric;

import static org.web3j.abi.TypeDecoder.instantiateType;
import static org.web3j.abi.TypeReference.makeTypeReference;

/**
 * Delegates to {@link DefaultFunctionEncoder} unless a {@link FunctionEncoderProvider} SPI is
 * found, in which case the first implementation found will be used.
 *
 * @see DefaultFunctionEncoder
 * @see FunctionEncoderProvider
 */
public abstract class FunctionEncoder {

    private static final FunctionEncoder FUNCTION_ENCODER;

    static {
        ServiceLoader<FunctionEncoderProvider> loader =
                ServiceLoader.load(FunctionEncoderProvider.class);
        final Iterator<FunctionEncoderProvider> iterator = loader.iterator();

        FUNCTION_ENCODER =
                iterator.hasNext() ? iterator.next().get() : new DefaultFunctionEncoder();
    }

    public static String encode(final Function function) {
        return FUNCTION_ENCODER.encodeFunction(function);
    }

    /** Encode function when we know function method Id / Selector. */
    public static String encode(final String methodId, final List<Type> parameters) {
        return FUNCTION_ENCODER.encodeWithSelector(methodId, parameters);
    }

    public static String encodeConstructor(final List<Type> parameters) {
        return FUNCTION_ENCODER.encodeParameters(parameters);
    }

    public static String encodeConstructorPacked(final List<Type> parameters) {
        return FUNCTION_ENCODER.encodePackedParameters(parameters);
    }

    public static Function makeFunction(
            String fnName,
            List<String> solidityInputTypes,
            List<Object> arguments,
            List<String> solidityOutputTypes)
            throws ClassNotFoundException,
                    NoSuchMethodException,
                    InstantiationException,
                    IllegalAccessException,
                    InvocationTargetException {
        List<Type> encodedInput = new ArrayList<>();
        Iterator argit = arguments.iterator();
        for (String st : solidityInputTypes) {
            encodedInput.add(instantiateType(st, argit.next()));
        }
        List<TypeReference<?>> encodedOutput = new ArrayList<>();
        for (String st : solidityOutputTypes) {
            encodedOutput.add(makeTypeReference(st));
        }
        return new Function(fnName, encodedInput, encodedOutput);
    }

    protected abstract String encodeFunction(Function function);

    protected abstract String encodeParameters(List<Type> parameters);

    /**
     * @param methodId Callback selector / Abi method Id (Hex format)
     */
    protected abstract String encodeWithSelector(
            final String methodId, final List<Type> parameters);

    protected abstract String encodePackedParameters(List<Type> parameters);

    protected static String buildMethodSignature(
            final String methodName, final List<Type> parameters) {

        final StringBuilder result = new StringBuilder();
        result.append(methodName);
        result.append("(");
        final String params =
                parameters.stream().map(Type::getTypeAsString).collect(Collectors.joining(","));
        result.append(params);
        result.append(")");
        return result.toString();
    }

    protected static String buildMethodId(final String methodSignature) {
        final byte[] input = methodSignature.getBytes();
        final byte[] hash = Hash.sha3(input);
        return Numeric.toHexString(hash).substring(0, 10);
    }
}
